<?php

namespace App\Containers\AppSection\Profile\UI\API\Transformers;

use App\Containers\AppSection\Accreditation\UI\API\Transformers\AccreditationTransformer;
use App\Containers\AppSection\Ban\Traits\HasBansTransformer;
use App\Containers\AppSection\BankDetail\UI\API\Transformers\BankDetailTransformer;
use App\Containers\AppSection\File\UI\API\Transformers\FileTransformer;
use App\Containers\AppSection\LegalForm\UI\API\Transformers\LegalFormTransformer;
use App\Containers\AppSection\Profile\Models\Profile;
use App\Containers\AppSection\Qualification\UI\API\Transformers\QualificationTransformer;
use App\Containers\AppSection\Status\UI\API\Transformers\StatusTransformer;
use App\Containers\AppSection\User\UI\API\Transformers\UserTransformer;
use App\Ship\Parents\Transformers\Transformer;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;

class ProfileTransformer extends Transformer
{
    use HasBansTransformer;

    protected array $defaultIncludes = [
        'accreditation_status', 'qualification_status'
    ];

    /**
     * @var  array
     */
    protected array $availableIncludes = [
        'legal_form',
        'bank_details',
        'accreditation',
        'accreditations',
        'user',
        'qualification',
        'qualifications',
        'ban',
        'bans',
        'avatar'
    ];

    public function transform(Profile $t): array
    {
        $response = [
            'object' => $t->getResourceKey(),
            'include' => $this->getAvailableIncludes(),
            'id' => $t->getHashedKey(),
            'phone' => $t->phone,
            'isBanned' => $t->isBanned(),
            'phone_verified_at' => $this->dateFormatter($t->phone_verified_at),
            'created_at' => $this->dateFormatter($t->created_at),
            'updated_at' => $this->dateFormatter($t->updated_at),
            'readable_created_at' => $t->created_at->diffForHumans(),
            'readable_updated_at' => $t->updated_at->diffForHumans(),
        ];

        if ($t->user->hasAnyRole('investor', 'borrower'))
            return array_merge($response, $this->outerProfile($t));

        return $response;
    }

    public function outerProfile(Profile $t): array
    {
        return [
            'fio' => $t->legal_form->personal->fio,
            'accreditation_status_key' => $t->accreditation_status->key,
            'accredited_at' => $this->dateFormatter($t->accredited_at),
            'qualification_status_key' => $t->qualification_status->key,
            'legal_form_type' => $t->legal_form_type,
        ];
    }

    public function includeLegalForm(Profile $t): ?Item
    {
        if ($t->user->hasAnyRole('investor', 'borrower'))
            return $this->nullableItem($t->legal_form, new LegalFormTransformer);
        else return null;
    }

    public function includeAccreditationStatus(Profile $t): ?Item
    {
        if ($t->user->hasAnyRole('investor', 'borrower'))
            return $this->nullableItem($t->accreditation_status, new StatusTransformer);
        else return null;

    }

    public function includeAvatar(Profile $t): ?Item
    {
        return $this->nullableItem($t->avatar, new FileTransformer);

    }

    public function includeBankDetails(Profile $t): ?Collection
    {
        if ($t->user->hasAnyRole('investor', 'borrower'))
            return $this->collection($t->bank_details, new BankDetailTransformer);
        else return null;
    }

    public function includeAccreditation(Profile $t): ?Item
    {
        if ($t->user->hasAnyRole('investor', 'borrower'))
            return $this->nullableItem($t->accreditation, new AccreditationTransformer);
        else return null;
    }

    public function includeAccreditations(Profile $t): ?Collection
    {
        if ($t->user->hasAnyRole('investor', 'borrower'))
            return $this->collection($t->accreditations, new AccreditationTransformer);
        else return null;
    }

    public function includeUser(Profile $t): Item
    {
        return $this->item($t->user, new UserTransformer);
    }

    public function includeQualificationStatus(Profile $t): ?Item
    {
        if ($t->hasLegalForm('indiv'))
            return $this->nullableItem($t->qualification_status, new StatusTransformer);
        else return null;
    }

    public function includeQualification(Profile $t): ?Item
    {
        if ($t->hasLegalForm('indiv'))
            return $this->nullableItem($t->qualification, new QualificationTransformer);
        else return null;
    }

    public function includeQualifications(Profile $t): ?Collection
    {
        if ($t->hasLegalForm('indiv'))
            return $this->collection($t->qualifications, new QualificationTransformer);
        else return null;
    }
}
